#coding:utf-8
import random

class Encryption():
    """
    * 类描述：密码加密、加密
    """
    def __init__(self):
        self.PW_Enc_Key = "xycqgwgkuvi87860988"


    def EncryptionEngine(self,Src,Key,Encrypt):
        """
        加密解密密码字符串。
        :param Src: String 为加密前的明文
        :param Key: 为密钥
        :param Encrypt: 为加、解密控制符,true为加密，反之解密
        :return:
        """
        KeyLen = 0
        KeyPos = 0
        offset = 0
        SrcPos = 0
        SrcAsc = 0
        TmpSrcAsc = 0
        Range = 0
        dest = ""
        KeyLen = len(Key)
        if KeyLen == 0:
            Key = self.PW_Enc_Key
        KeyPos = -1
        SrcPos = 0
        SrcAsc = 0
        Range = 256
        if Encrypt == True:
            r1 = random.Random()
            offset = r1.randint(0,256)
            if offset >= 16:
                dest = str(hex(offset)).split("0x")[1]
            else:
                dest = "0" + str(hex(offset)).split("0x")[1]
            for i in range(0, len(Src)):
                SrcAsc = (ord(Src[i])+offset)%255
                if KeyPos < KeyLen -1:
                    KeyPos = KeyPos + 1
                else:
                    KeyPos = 0
                SrcAsc = SrcAsc ^ ord(Key[KeyPos])
                if SrcAsc >= 16:
                    dest = dest + str(hex(SrcAsc)).split("0x")[1]
                else:
                    dest = dest + "0" + str(hex(SrcAsc)).split("0x")[1]
                offset = SrcAsc
        else:
            if(len(Src)==2):
                return ""
            offset = self.HexToInt(Src[0:2])
            SrcPos = 2
            while True:
                SrcAsc = self.HexToInt(Src[SrcPos:SrcPos+2])
                if KeyPos < KeyLen -1:
                    KeyPos = KeyPos +1
                else:
                    KeyPos = 0
                TmpSrcAsc = SrcAsc ^ ord(Key[KeyPos])
                if TmpSrcAsc <= offset:
                    TmpSrcAsc = 255 + TmpSrcAsc -offset
                else:
                    TmpSrcAsc = TmpSrcAsc - offset
                dest = dest + chr(TmpSrcAsc)
                offset = SrcAsc
                SrcPos = SrcPos +2
                if SrcPos >= len(Src) - 1:
                    break
        return dest

    def HexToInt(self,HexStr):
        Result = 0
        for char in HexStr:

            char = ord(char)
            if char >= 97:
                Result = Result * 16 + char - 87
            elif char >= 65:
                Result = Result*16 + char -55
            else:
                Result = Result * 16 + char - 48
        return Result



import hashlib

class Verify_Md5():
    def __init__(self):
        self.md = hashlib.md5()
    def savaString2File(self,content, fileName):#暂时不要
        return
    def getHash(self,filename):
        #打开文件
        try:
            src = open(filename)
        except:
            print "file not exist."
            return
        stream = src.read()
        self.md.update(stream)
        return self.md.hexdigest()



if __name__ == "__main__":
    HexStr = "Boo"
    HexStr = list(HexStr)
    enc = Encryption()

    dest = enc.EncryptionEngine("950521","",True)
    print dest
    print enc.EncryptionEngine(dest,"",False)
    #print enc.HexToInt(HexStr)
    # r1 = random.randint(0,2^31-1)
    r1 = random.Random(256)
    offset = r1.randint(0,2^31-1)
    #print offset
    #print hex(offset),str(hex(offset)).split("0x")[1]
    #print char,type(char)

    hashmd5 = Verify_Md5()
    print hashmd5.getHash("md5.txt")